package com.sfeir.poc.portal.gui.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.sfeir.poc.portal.gui.client.events.ApplicationEvent;
import com.sfeir.poc.portal.gui.client.events.ApplicationEventType;
import com.sfeir.poc.portal.gui.client.objects.Position;
import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;
import com.sfeir.poc.portal.gui.client.util.RssContentParser;

/**
 * 
 * @author bostoen.v
 * This class is a factory of WidgetDescriptions
 * It constructs WidgetDescription objects from List<String[]> Object
 * and keeps a reference to
 *
 */
public class WidgetFactory extends EventAwareClass {
	
	private RssContentParser parser = new RssContentParser();
	private HashMap<Position, WidgetDescription> widgetsDescriptions = new HashMap<Position, WidgetDescription>();
	
	public HashMap<Position, WidgetDescription> getWidgetsDescriptions() {
		return widgetsDescriptions;
	}

	private void updateAWidgetDescription(WidgetDescription aWidgetDescription) {
		final Position position = new Position();
		position.setX(aWidgetDescription.getColumn());
		widgetsDescriptions.put(position, aWidgetDescription);

		fireActionEvent(ApplicationEventType.UPDATE_WIDGETS, getWidgetsDescriptions());
	}

	private void buildWidgetsDescriptions(List<String[]> widgetsList) {
		widgetsDescriptions.clear();
		
		String[] strings = null;
		Position position = null;
		WidgetDescription currentWidgetDescription = null;
		Vector<String> args = null;
		for (Iterator<String[]> iterator = widgetsList.iterator(); iterator.hasNext();) {
			strings = (String[]) iterator.next();
			position = new Position();
			position.setX(new Integer(strings[1]));
			
			currentWidgetDescription = new WidgetDescription();
			currentWidgetDescription.setColumn(new Integer(strings[1]));
			currentWidgetDescription.setType(strings[2]);
			currentWidgetDescription.setTitle(strings[0]);
			currentWidgetDescription.setUrl(strings[3]);
			args = new Vector<String>();
			for (int i = 4; i < strings.length; i++) {
				if (strings[i] != null) {
					args.add(strings[i]);
				}
			}
			if (args.size() > 0) {
				currentWidgetDescription.setArgs(args);
			}
			
			if(currentWidgetDescription.getType().compareTo("spreadsheet") == 0) {
				fireActionEvent(ApplicationEventType.RETRIEVE_FEED, currentWidgetDescription);
			} else if (currentWidgetDescription.getType().compareTo("rss") == 0) {
					fireActionEvent(ApplicationEventType.RETRIEVE_FEED, currentWidgetDescription);
			} else if (currentWidgetDescription.getType().compareTo("slideshow") == 0) {
					fireActionEvent(ApplicationEventType.RETRIEVE_FEED, currentWidgetDescription);
			} else {
				widgetsDescriptions.put(position, currentWidgetDescription);
			}
		}
		fireActionEvent(ApplicationEventType.UPDATE_WIDGETS, getWidgetsDescriptions());
	}
	
	public void onActionFired(ApplicationEvent event) {
		super.onActionFired(event);
		if (event.getType().equals(ApplicationEventType.CREATE_WIDGETS)) {
			List<String[]> widgetsList = parser.parse((String) event.getContext());
			buildWidgetsDescriptions(widgetsList);
		} else if(event.getType().equals(ApplicationEventType.UPDATE_FACTORY)) {
			updateAWidgetDescription((WidgetDescription) event.getContext());
		}
	}
}
